package com.luoye.lj.util.number;

import java.util.Date;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-12-3
 * Time: 下午6:27
 * To change this template use File | Settings | File Templates.
 */
public class NumberUtil {
    // 任意类型转整形
    public static int toInteger(String n) {
        int val=0;
        try{
            val= Integer.parseInt(n);
        }catch (NumberFormatException ex){}
        return val;
    }

    // 任意类型转长整形
    public static long toLong(String n) {
        long val=0;
        try{
            val= Long.parseLong(n);
        }catch (NumberFormatException ex){}
        return val;
    }
    //任意类型转换为double
    public static double toDouble(String n) {
        double val=0;
        try{
            val= Double.parseDouble(n);
        }catch (NumberFormatException ex){}
        return val;
    }
    //任意类型转换为double
    public static float toFloat(String n) {
        float val=0;
        try{
            val= Float.parseFloat(n);
        }catch (NumberFormatException ex){}
        return val;
    }
    /**********
     * 创建一个数据记录的编号num，由时间加4位随机数组成
     * ***********/
    public static long makeNum(){
        Date date=new Date();
        long time = new Date().getTime() ;
        StringBuffer temp=new StringBuffer("");
        for(int i=0;i<4;i++){
            temp.append(new Random().nextInt(10));
        }
        String timestr=time+"";
        String numStr = timestr+temp.toString();
        long num = 0;
        try{
            num= Long.parseLong(numStr);
        }catch (NumberFormatException ex){}
        return num;
    }
}
